package com.stylefeng.guns.rest.modular.wz.api.v3.service.impl;

import com.stylefeng.guns.rest.common.constant.Consts;
import com.tls.sigcheck.tls_sigcheck;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

@Service
public class CreateUserSigImpl {

    public String getUserSig(String userCode) throws Exception {

        tls_sigcheck demo = new tls_sigcheck();
        // 使用前请修改动态库的加载路径
        File priKeyFile = null;
        if(Consts.dev.equals("true")){
            demo.loadJniLib("C:\\wz\\jnisigcheck.dll");
            priKeyFile = new File("C:\\wz\\private_key");
        }else{
            demo.loadJniLib("/usr/local/wz/jnisigcheck.so");
            priKeyFile = new File("/usr/local/wz/private_key");
        }
        StringBuilder strBuilder = new StringBuilder();
        String s = "";

        BufferedReader br = new BufferedReader(new FileReader(priKeyFile));
        while ((s = br.readLine()) != null) {
            strBuilder.append(s + '\n');
        }
        br.close();
        String priKey = strBuilder.toString();
        int ret = demo.tls_gen_signature_ex2(Consts.sdkappid, userCode, priKey);
//        int ret=demo.tls_gen_signature_ex2_with_expire("1400155242",userCode,priKey,"10");

        if (0 != ret) {
//            System.out.println("ret " + ret + " " + demo.getErrMsg());
            return  null;
        }
        else
        {
//            File pubKeyFile = new File("C:\\Users\\Administrator\\Desktop\\public_key");
//            br = new BufferedReader(new FileReader(pubKeyFile));
//            strBuilder.setLength(0);
//            while ((s = br.readLine()) != null) {
//                strBuilder.append(s + '\n');
//            }
//            br.close();
//            String pubKey = strBuilder.toString();
//            ret = demo.tls_check_signature_ex2(demo.getSig(), pubKey, "1400155242", userCode);
//            if (0 != ret) {
//                System.out.println("ret " + ret + " " + demo.getErrMsg());
//            }
//            else
//            {
//                System.out.println("--\nverify ok -- expire time " + demo.getExpireTime() + " -- init time " + demo.getInitTime());
//            }
//            System.out.println("sig:\n" + demo.getSig());
            return demo.getSig();
        }

//        File pubKeyFile = new File("C:\\Users\\Administrator\\Desktop\\public_key");
//        br = new BufferedReader(new FileReader(pubKeyFile));
//        strBuilder.setLength(0);
//        while ((s = br.readLine()) != null) {
//            strBuilder.append(s + '\n');
//        }
//        br.close();
//        String pubKey = strBuilder.toString();
//        ret = demo.tls_check_signature_ex2(demo.getSig(), pubKey, "1400155242", "xiaojun");
//        if (0 != ret) {
//            System.out.println("ret " + ret + " " + demo.getErrMsg());
//        }
//        else
//        {
//            System.out.println("--\nverify ok -- expire time " + demo.getExpireTime() + " -- init time " + demo.getInitTime());
//        }
    }
}

